<?php
class GameController extends CpController
{
    public function init(){
        $url = new Url();
        $this->breadcrumbs[] = array('name'=>'QT Game','link'=>$url->createUrl("game/index"),'class'=>'');
    }
    public function actionIndex(){   
        $row_per_page = 20; $arr_game_id = array();  
        $this->breadcrumbs[] = array('name'=>'DS Game','link'=>'#','class'=>'active');
        $from_date = isset($_GET["from_date"]) ? trim($_GET["from_date"]):"";
        $to_date = isset($_GET["to_date"]) ? trim($_GET["to_date"]):"";
        $categoryId = isset($_GET["categoryId"]) ? intval($_GET["categoryId"]):0;
        $keyword = isset($_GET["keyword"]) ? trim($_GET["keyword"]):"";
        $business_name = isset($_GET["business_name"]) ? trim($_GET["business_name"]):"";
        $partnerId = isset($_GET["partnerId"]) ? intval($_GET["partnerId"]):0;
        $percent_partner = isset($_GET["percent_partner"]) ? intval($_GET["percent_partner"]):0;
        $percent_wapmaster = isset($_GET["percent_wapmaster"]) ? intval($_GET["percent_wapmaster"]):0;
        $type = isset($_GET["type"]) ? intval($_GET["type"]):0;
        $status = isset($_GET["status"]) ? intval($_GET["status"]):2;
        $isHot = isset($_GET["isHot"]) ? intval($_GET["isHot"]):2;
        $isHome = isset($_GET["isHome"]) ? intval($_GET["isHome"]):2;
        $isHotApp = isset($_GET["isHotApp"]) ? intval($_GET["isHotApp"]):2;
        $order_download = isset($_GET["order_download"]) ? intval($_GET["order_download"]):0;
        $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
        $data_cat = ACategory::getAllData(1);
        list($max_page,$count,$data) = AGame::getDataSearch($from_date,$to_date,$categoryId,$keyword,$type,$status,$isHot,$page,$row_per_page,$business_name,$partnerId,$percent_partner,$percent_wapmaster,$isHome,$isHotApp,$order_download);
        for($i=0;$i<count($data);$i++){
            $arr_game_id[] = $data[$i]["id"];
            $data[$i]["count_file"] = 0;
        }
        $data_count_file = AGameFile::getCountDataGroupByArrGame($arr_game_id);
        for($i=0;$i<count($data);$i++){
            for($j=0;$j<count($data_count_file);$j++){ 
                if($data[$i]["id"] == $data_count_file[$j]["gameId"]){
                    $data[$i]["count_file"] = $data_count_file[$j]["count"];
                }
            }
        }
        $this->render("index",array("data_cat"=>$data_cat,"max_page"=>$max_page,"count"=>$count,"data"=>$data,
        "from_date"=>$from_date,"to_date"=>$to_date,"categoryId"=>$categoryId,"keyword"=>$keyword,"type"=>$type,
        "business_name"=>$business_name,"partnerId"=>$partnerId,"percent_partner"=>$percent_partner,
        "percent_wapmaster"=>$percent_wapmaster,"isHome"=>$isHome,"isHotApp"=>$isHotApp,"order_download"=>$order_download,
        "status"=>$status,"isHot"=>$isHot,"page"=>$page));
    }
    public function actionCreate(){
        $this->breadcrumbs[] = array('name'=>'Thêm mới game','link'=>'#','class'=>'active');
        $data_cat = ACategory::getAllData(1);
        $data_handset = AMobileHandset::getDataGroupManufacturer();
        $this->render("create",array("data_cat"=>$data_cat,"data_handset"=>$data_handset));
    }
    public function actionAjaxSaveGame(){
        $time_now = time();
        $create_user = Yii::app()->user->name;
        $title_game = isset($_POST["title_game"]) ? trim(strip_tags($_POST["title_game"])):"";
        $title_sms = isset($_POST["title_sms"]) ? trim(strip_tags($_POST["title_sms"])):"";
        $categoryId = isset($_POST["categoryId"]) ? intval($_POST["categoryId"]):0;
        $picture = isset($_POST["picture"]) ? trim($_POST["picture"]):"";
        $introtext = isset($_POST["introtext"]) ? trim(strip_tags($_POST["introtext"])):"";   
        $description = isset($_POST["description"]) ? trim($_POST["description"]):"";   
        $isOnline = isset($_POST["isOnline"]) ? intval($_POST["isOnline"]):0;
        $isSms = isset($_POST["isSms"]) ? intval($_POST["isSms"]):0;
        $type_game = isset($_POST["type_game"]) ? intval($_POST["type_game"]):0;
        $filename = isset($_POST["filename"]) ? trim($_POST["filename"]):"";
        $filesize = isset($_POST["filesize"]) ? trim($_POST["filesize"]):"";
        $extension = isset($_POST["extension"]) ? trim($_POST["extension"]):"";
        $introtext_file = isset($_POST["introtext_file"]) ? trim($_POST["introtext_file"]):"";  
        $linkId = isset($_POST["linkId"]) ? intval($_POST["linkId"]):0;  
        $supplier = isset($_POST["supplier"]) ? intval($_POST["supplier"]):0;  
        $device = isset($_POST["device"]) ? rtrim($_POST["device"],";"):"";
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $position = isset($_POST["position"]) ? intval($_POST["position"]):0;
        $business_name = isset($_POST["business_name"]) ? trim($_POST["business_name"]):"";
        $partnerId = isset($_POST["partnerId"]) ? intval($_POST["partnerId"]):0;
        $percent_partner = isset($_POST["percent_partner"]) ? intval($_POST["percent_partner"]):0;
        $percent_telco = isset($_POST["percent_telco"]) ? $_POST["percent_telco"]:1;
        $percent_wapmaster = isset($_POST["percent_wapmaster"]) ? intval($_POST["percent_wapmaster"]):0;
        $isBuild = isset($_POST["isBuild"]) ? intval($_POST["isBuild"]):0;
        $tag = isset($_POST["tag"]) ? trim(strip_tags($_POST["tag"])):""; 
        if($title_game == ""){echo "Tên game không được để trống";exit;}
        if($title_sms == ""){echo "Tiêu đề tin nhắn không được để trống";exit;}
        if(Common::generate_slug($title_game)==''){
            echo "Tên game không hợp lệ";exit;
        }
        if($percent_partner <0 && $percent_partner >100){echo "Chiết khấu cho nhà cung cấp phải lớn hơn 0 và nhỏ hơn 100";exit;}
        if($percent_wapmaster <0 && $percent_wapmaster >100){echo "Chiết khấu cho wap master phải lớn hơn 0 và nhỏ hơn 100";exit;}
        if($categoryId == 0){echo "Chưa chọn danh mục cho game";exit;}
        if($picture == ""){echo "Chưa chọn ảnh đại diện cho game";exit;}
        if($type_game==0){
            if($filename == ""){echo "Chưa upload file cho game";exit;}
        }else{
            if($linkId <=0){echo "Chưa nhập mã link file game";exit;}
            if($supplier <=0){echo "Chưa chọn nhà cung cấp";exit;}
        }
        if($device == ""){echo "Chưa chọn thiết bị cho file game này";exit;}
        $data_cat = ACategory::getDataById($categoryId);
        if(!$data_cat){ echo "Không tồn tại danh mục này";exit;}

        /* them moi game*/
        $data_game = array(
        "categoryId"=>array("value"=>$categoryId,"type"=>1),
        "title"=>array("value"=>$title_game,"type"=>2),
        "title_sms"=>array("value"=>$title_sms,"type"=>2),
        "alias"=>array("value"=>Common::generate_slug($title_game),"type"=>2),
        "picture"=>array("value"=>$picture,"type"=>2),
        "introtext"=>array("value"=>$introtext,"type"=>2),
        "description"=>array("value"=>$description,"type"=>2),
        "position"=>array("value"=>$position,"type"=>1),
        "type"=>array("value"=>$type_game,"type"=>1),
        "create_date"=>array("value"=>$time_now,"type"=>1),
        "update_date"=>array("value"=>$time_now,"type"=>1),
        "isOnline"=>array("value"=>$isOnline,"type"=>1),
        "business_name"=>array("value"=>$business_name,"type"=>2),
        "partnerId"=>array("value"=>$partnerId,"type"=>1),
        "percent_partner"=>array("value"=>round($percent_partner/$percent_telco,2),"type"=>1),
        "percent_wapmaster"=>array("value"=>$percent_wapmaster,"type"=>1),
        "isBuild"=>array("value"=>$isBuild,"type"=>1),
        "create_user"=>array("value"=>$create_user,"type"=>2),
        );
        $arr_ext_game = array("ipa","apk");
        if(in_array($extension,$arr_ext_game)){
            $data["status"] = array("value"=>$status,"type"=>1);
        }else{
            $data["status"] = array("value"=>0,"type"=>1);
        }
        if($isOnline == 1){
            $data_game["date_of_online"] = array("value"=>$time_now,"type"=>1);
        }else{
            $data_game["isSms"] = array("value"=>$isSms,"type"=>1);
        }
        switch($extension){
            case 'apk':
                $data_game["isAndroid"] = array("value"=>1,"type"=>1);
                break;
            case 'cod':
                $data_game["isRim"] = array("value"=>1,"type"=>1);
                break;
            case 'ipa':
                $data_game["isIos"] = array("value"=>1,"type"=>1);
                break;
        }
        $table = "c_game";
        $gameId = CommonDB::insertRow($table,$data_game);
        
        /* them trong bang c_data*/        
        $data_c = array(
        "categoryId"=>array("value"=>$data_cat["id"],"type"=>1),
        "type"=>array("value"=>$data_cat["type"],"type"=>1),
        "dataId"=>array("value"=>$gameId,"type"=>1),
        "status"=>array("value"=>1,"type"=>1),
        "create_date"=>array("value"=>$time_now,"type"=>1),
        "update_date"=>array("value"=>$time_now,"type"=>1),
        );
        $table = "c_data";
        $data_id = CommonDB::insertRow($table,$data_c);
        if($type_game==0){
            $result = AGameFile::insertGameFile($gameId,$filename,$extension,$filesize,$introtext_file,$time_now,$device);
        }else{
            $result = AGameFile::insertGameFile($gameId,"","","","",$time_now,$device,$linkId,$supplier);
        }
        if($result ==-1){
            echo "Chưa thêm được file game hoặc thiết bị cho file game! Hãy kiểm tra lại";exit;
        }
        if($gameId >0){
            /* Xử lý tag */
            if(!empty($tag)){
                $tag = explode(";", $tag);
                $result = ATag::InsertTag($tag, 1, $gameId);
            }
            
            $act = 1;
            $obj = 2;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$gameId.',name: '.$title_game.',filegame: '.$filename.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;
        }else{
            echo "Có lỗi trong quá trình xử lý! Hãy báo với kỹ thuật";exit;
        } 
    }
    public function actionAjaxSaveGameFile(){

        $gameId = isset($_POST["gameId"]) ? intval($_POST["gameId"]):0; 
        $type_game = isset($_POST["type_game"]) ? intval($_POST["type_game"]):0;          
        $filename = isset($_POST["filename"]) ? trim($_POST["filename"]):"";
        $filesize = isset($_POST["filesize"]) ? trim($_POST["filesize"]):"";
        $extension = isset($_POST["extension"]) ? trim($_POST["extension"]):"";
        $introtext_file = isset($_POST["introtext_file"]) ? trim($_POST["introtext_file"]):"";
        $linkId = isset($_POST["linkId"]) ? intval($_POST["linkId"]):0; 
        $supplier = isset($_POST["supplier"]) ? intval($_POST["supplier"]):0;
        $device = isset($_POST["device"]) ? rtrim($_POST["device"],";"):"";
        if($type_game==0){
            if($filename == ""){echo "Chưa upload file cho game";exit;}
        }else{
            if($linkId <=0){echo "Chưa nhập mã link file game";exit;}
            if($supplier <=0){echo "Chưa chọn nhà cung cấp";exit;}
        }
        if($device == ""){echo "Chưa chọn thiết bị cho file game này";exit;}
        $data_game = AGame::getDataById($gameId);
        $time_now = $data_game["create_date"];

        switch($extension){
            case 'apk':  
                $arr_game = array(
                "isAndroid"=>array("value"=>1,"type"=>1),
                "id"=>array("value"=>$gameId,"type"=>1),
                );   
                $table = "c_game"; 
                $result = CommonDB::updateRow($table,$arr_game,array("id"));
                break;
            case 'cod':
                $arr_game = array(
                "isRim"=>array("value"=>1,"type"=>1),
                "id"=>array("value"=>$gameId,"type"=>1),
                );
                $table = "c_game"; 
                $result = CommonDB::updateRow($table,$arr_game,array("id"));
                break;
            case 'ipa':
                $arr_game = array(
                "isIos"=>array("value"=>1,"type"=>1),
                "id"=>array("value"=>$gameId,"type"=>1),
                );
                $table = "c_game"; 
                $result = CommonDB::updateRow($table,$arr_game,array("id"));
                break;
        }
        if($type_game==0){
            $result = AGameFile::insertGameFile($gameId,$filename,$extension,$filesize,$introtext_file,$time_now,$device);
        }else{
            $result = AGameFile::insertGameFile($gameId,"","","","",$time_now,$device,$linkId,$supplier);
        }
        if($result ==-1){
            echo "Chưa thêm được file game hoặc thiết bị cho file game! Hãy kiểm tra lại";exit;
        }
        if($gameId >0){
            $act = 2;
            $obj = 2;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': thêm file game {gameId: '.$gameId.',name: '.$data_game["title"].',filegame: '.$filename.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            $check_game = AGameFile::checkFileGame($gameId);
            if($check_game==false){
                echo 2;exit();
            }
            echo 1;
        }else{
            echo "Có lỗi trong quá trình xử lý! Hãy báo với kỹ thuật";exit;
        }
    }
    public function actionAjaxQuickUpdate(){
        $time_now = time();
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $isHot = isset($_POST["isHot"]) ? intval($_POST["isHot"]):0;
        $isHome = isset($_POST["isHome"]) ? intval($_POST["isHome"]):0;
        $manySms = isset($_POST["manySms"]) ? intval($_POST["manySms"]):0;
        $manyCard = isset($_POST["manyCard"]) ? intval($_POST["manyCard"]):0;
        $download = isset($_POST["download"]) ? intval($_POST["download"]):0;
        $hit = isset($_POST["hit"]) ? intval($_POST["hit"]):0;
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){   

            $data_old = AGame::getDataById($id);
            $result = AGameFile::updateStatusByGameId($id,$status);
            $result = AData::updateStatusByDataId($id,$status);
            $data = array(
            "isHot"=>array("value"=>$isHot,"type"=>1),
            "isHome"=>array("value"=>$isHome,"type"=>1),
            "manySms"=>array("value"=>$manySms,"type"=>1),
            "manyCard"=>array("value"=>$manyCard,"type"=>1),
            "status"=>array("value"=>$status,"type"=>1),
            "download"=>array("value"=>$download,"type"=>1),
            "hit"=>array("value"=>$hit,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1)
            );

            if($isHot ==1){
                $data["date_of_hot"] = array("value"=>$time_now,"type"=>1);
            }            
            if($isHome ==1){
                $data["date_of_home"] = array("value"=>$time_now,"type"=>1);
            }
            $table = "c_game";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 2;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {gameId: '.$id.',name: '.$data_old["title"].',isHot: '.$data_old["isHot"].',isHome: '.$data_old["isHome"].',status: '.$data_old["status"].'} thành';
                $content .= '{gameId: '.$id.',name: '.$data_old["title"].',isHot: '.$isHot.',isHome: '.$isHome.',status: '.$status.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);  
                $check_game = AGameFile::checkFileGame($id);
                if($check_game==false){
                    echo "Game bạn upload chưa đủ cặp file đuôi (jar,jad)!Game này sẽ bị ẩn";exit();
                }              
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionAjaxQuickUpdateFileGame(){
        $time_now = time();
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;        
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){   

            $data_old = AGame::getDataById($id);
            
            $data = array(
                "status"=>array("value"=>$status,"type"=>1),
                "id"=>array("value"=>$id,"type"=>1)
            );
            
            $table = "c_game_file";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 2;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {gameId: '.$id.',status: '.$data_old["status"].'} thành';
                $content .= '{gameId: '.$id.',status: '.$status.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);  
                $check_game = AGameFile::checkFileGame($id);
                
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionEdit(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $data = AGame::getDataById($id);
        $this->breadcrumbs[] = array('name'=>'Sửa game '.$data["title"],'link'=>'#','class'=>'active');
        if($data){
            $data_cat = ACategory::getAllData(1);
            $this->render("edit",array("data"=>$data,"data_cat"=>$data_cat));
        }
    }
    public function actionAjaxUpdateGame(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $title_game = isset($_POST["title_game"]) ? trim(strip_tags($_POST["title_game"])):"";
        $title_sms = isset($_POST["title_sms"]) ? trim(strip_tags($_POST["title_sms"])):"";
        $tag = isset($_POST["tag"]) ? trim(strip_tags($_POST["tag"])):"";
        $categoryId = isset($_POST["categoryId"]) ? intval($_POST["categoryId"]):0;
        $position = isset($_POST["position"]) ? intval($_POST["position"]):0;
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $picture = isset($_POST["picture"]) ? trim($_POST["picture"]):"";
        $introtext = isset($_POST["introtext"]) ? trim(strip_tags($_POST["introtext"])):"";
        $description = isset($_POST["description"]) ? trim($_POST["description"]):"";
        $partnerId = isset($_POST["partnerId"]) ? intval($_POST["partnerId"]):0;
        $percent_partner = isset($_POST["percent_partner"]) ? intval($_POST["percent_partner"]):0;
        $percent_telco = isset($_POST["percent_telco"]) ? $_POST["percent_telco"]:1;
        $percent_wapmaster = isset($_POST["percent_wapmaster"]) ? intval($_POST["percent_wapmaster"]):0;
        if($title_game == ""){echo "Tên game không được để trống";exit;}
        if($title_sms == ""){echo "Tiêu đề tin nhắn không được để trống";exit;}
        if($categoryId == 0){echo "Chưa chọn danh mục cho game";exit;}
        if($picture == ""){echo "Chưa chọn ảnh đại diện cho game";exit;}
        if($percent_partner <0 && $percent_partner >100){echo "Chiết khấu cho nhà cung cấp phải lớn hơn 0 và nhỏ hơn 100";exit;}
        if($percent_wapmaster <0 && $percent_wapmaster >100){echo "Chiết khấu cho wap master phải lớn hơn 0 và nhỏ hơn 100";exit;}
        $data_game = AGame::getDataById($id);
        if($data_game){

            if($data_game["categoryId"] != $categoryId){
                $result = AData::updateCategoryByDataId($id,$categoryId);
            }
            $data = array(
            "categoryId"=>array("value"=>$categoryId,"type"=>1),
            "title"=>array("value"=>$title_game,"type"=>2),      
            "title_sms"=>array("value"=>$title_sms,"type"=>2),      
            "picture"=>array("value"=>$picture,"type"=>2),
            "position"=>array("value"=>$position,"type"=>1),
            "status"=>array("value"=>$status,"type"=>1),
            "introtext"=>array("value"=>$introtext,"type"=>2),
            "description"=>array("value"=>$description,"type"=>2),
            "partnerId"=>array("value"=>$partnerId,"type"=>1),            
            "update_date"=>array("value"=>$time_now,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1),
            );
            if(round($percent_partner/$percent_telco,2) >0){
                $data["percent_partner"]=array("value"=>round($percent_partner/$percent_telco,2),"type"=>1);            
            }
            if($percent_wapmaster >0){
                $data["percent_wapmaster"]=array("value"=>$percent_wapmaster,"type"=>1);
            }
            $table = "c_game";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                /* Xử lý tag */
                $result = ATag::UpdateTag($tag, 1, $id);
                
                $act = 2;
                $obj = 2;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {gameId: '.$id.',name: '.$data_game["title"].',categoryId: '.$data_game["categoryId"].'} thành';
                $content .= '{gameId: '.$id.',name: '.$title_game.',categoryId: '.$categoryId.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);   
                $check_game = AGameFile::checkFileGame($id);
                if($check_game==false){
                    echo 2;exit();
                }            
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }

    public function actionAjaxDelete(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $arr_file_game = array();
            $data_old = AGame::getDataById($id);
            $data_file = AGameFile::getDataByGame($id);
            for($i=0;$i<count($data_file);$i++){
                $arr_file_game[] = $data_file[$i]["id"];
            }
            $table  = "c_game_file_mobile_handset";
            $result = CommonDB::deleteManyRows($table,"gameFileId",$arr_file_game);
            $table  = "c_data";
            $result = CommonDB::deleteRow($table,array("dataId"=>$id));
            $table = "c_game_file";
            $result = CommonDB::deleteRow($table,array("gameId"=>$id));
            $table = "c_game";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 2;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {gameId: '.$id.',name: '.$data_old["title"].'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionGameFile(){
        $arr_file = array();
        $gameId = isset($_GET["gameId"]) ? intval($_GET["gameId"]):0;
        $data_game = AGame::getDataById($gameId);
        if($data_game){
            $data = AGameFile::getDataByGame($gameId);
            $this->breadcrumbs[] = array('name'=>'DS file game của game: '.$data_game["title"],'link'=>'#','class'=>'active');
            for($i=0;$i<count($data);$i++){
                $data[$i]["handset"] = "";
                $arr_file[]  =  $data[$i]["id"];
            }
            $data_handset = AGameFileMobileHandset::getDataByArrGameFile($arr_file);
            for($i=0;$i<count($data);$i++){
                for($j=0;$j<count($data_handset);$j++){
                    if($data[$i]["id"] == $data_handset[$j]["gameFileId"]){
                        $data[$i]["handset"] .= $data_handset[$j]["manufacturer"]." ".$data_handset[$j]["handset"].',';
                    }
                }
            }
            $this->render("game_file",array("data"=>$data,"gameId"=>$gameId,"data_game"=>$data_game));
        }
    }

    public function actionSearchHandset(){
        $arr_hs = AMobileHandset::getAllData(2);
        $data_handset = AMobileHandset::getDataGroupManufacturer();
        $this->renderPartial("search_handset",array("data_handset"=>$data_handset,"arr_hs"=>$arr_hs));
    }
    public function actionAjaxSearchHandset(){
        $keyword = isset($_POST["keyword"]) ? trim(strip_tags($_POST["keyword"])):"";
        $manufacturer = isset($_POST["manufacturer"]) ? trim($_POST["manufacturer"]):"";
        if($manufacturer !=""){
            $data_search = AMobileHandset::getDataByManufacturerAndKeyword($manufacturer,$keyword);
            if($data_search){ 
                for($i=0;$i<count($data_search);$i++){
                    echo '<li id="m_handset_'.$data_search[$i]["id"].'"><input type="checkbox" value="'.$data_search[$i]["id"].'" name="check_id_hs"> '.$data_search[$i]["manufacturer"].' '.$data_search[$i]["handset"].' <a href="javascript:void(0)" onclick="setOneHandset('.$data_search[$i]["id"].');" class="add">&nbsp;</a></li>';
                }
            }else{
                echo '<li>Không tìm thấy thiết bị</li>';
            }
        }
    }

    public function actionCreateGameFile(){
        $gameId = isset($_GET["gameId"]) ? intval($_GET["gameId"]):0;
        $data_game = AGame::getDataById($gameId);
        if($data_game){
            $this->breadcrumbs[] = array('name'=>'Thêm mới file game','link'=>'#','class'=>'active');
            $data_handset = AMobileHandset::getDataGroupManufacturer();
            if($data_game["type"]==0){                
                $this->render("create_game_file",array("data_handset"=>$data_handset,"data_game"=>$data_game));
            }else{
                $this->render("create_game_link",array("data_handset"=>$data_handset,"data_game"=>$data_game));
            }
        }
    }

    public function actionEditGameFile(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $gameId = isset($_GET["gameId"]) ? intval($_GET["gameId"]):0;
        $data_game = AGame::getDataById($gameId);
        $data_game_file = AGameFile::getDataById($id);
        if($data_game && $data_game_file){
            $this->breadcrumbs[] = array('name'=>'Sửa file game','link'=>'#','class'=>'active'); 
            $data_handset = AMobileHandset::getAllData(2);
            $data_game_hs = AGameFileMobileHandset::getDataByGameFile($id);
            $this->render("edit_game_file",array("data_game_file"=>$data_game_file,"data_handset"=>$data_handset,
            "data_game_hs"=>$data_game_hs,"data_game"=>$data_game));
        }
    }

    public function actionAjaxUpdateGameFile(){
        $result = 0;
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0; 
        $introtext = isset($_POST["introtext"]) ? trim(strip_tags($_POST["introtext"])):"";
        $device = isset($_POST["device"]) ? rtrim($_POST["device"],";"):"";
        if($id >0){
            $data_old = AGameFile::getDataById($id);
            if($device == ""){echo "Chưa chọn thiết bị cho file game này";exit;}
            $gameFileId = $id;
            /* update intro text*/
            $data = array(
            "introtext"=>array("value"=>$introtext,"type"=>2),
            "update_date"=>array("value"=>$time_now,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1),
            );
            $table = "c_game_file";
            $result = CommonDB::updateRow($table,$data,array("id"));
            $arr_handset = explode(";",$device);
            $data_handset = AMobileHandset::getAllData(1);
            /* xoa game mobile handset*/
            $table = "c_game_file_mobile_handset";
            $result = CommonDB::deleteRow($table,array("gameFileId"=>$gameFileId));
            /* them game mobile handset*/
            for($i=0;$i<count($arr_handset);$i++){
                if(intval($arr_handset[$i]) > 0){
                    $mobileHandsetId = intval($arr_handset[$i]);
                    $data_game_handset = AGameFileMobileHandset::getDataByFileAndHandset($gameFileId,$mobileHandsetId);
                    if(!$data_game_handset){
                        $last_id = AGameFileMobileHandset::insertData($gameFileId,$mobileHandsetId);
                        $result++;
                    }
                }
            }
            if($result >0){
                $act = 2;
                $obj = 2;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].'(sửa dòng máy file game): {gameId: '.$data_old["gameId"].',gameFileId: '.$id.',filename: '.$data_old["filename"].'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;
            }else{
                echo "Có vấn đề xảy ra! Vui lòng thử lại";
            }
        }
    }

    public function actionAjaxDeleteGameFile(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = AGameFile::getDataById($id);
            $table  = "c_game_file_mobile_handset";
            $result = CommonDB::deleteRow($table,array("gameFileId"=>$id));
            $table  = "c_game_file";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 2;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].'(file game): {gameId: '.$data_old["gameId"].',gameFileId: '.$id.',filename: '.$data_old["filename"].'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                $check_game = AGameFile::checkFileGame($data_old["gameId"]);
                if($check_game==false){
                    echo "Game bạn upload chưa đủ cặp file đuôi (jar,jad)!Game này sẽ bị ẩn";exit();
                }
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
}
